package com.cljz.makepassenger.present.activity;


import com.cljz.makepassenger.base.App;
import com.cljz.makepassenger.base.BaseP;
import com.cljz.makepassenger.model.net.api.Api;
import com.cljz.makepassenger.model.net.bean.About1qBean;
import com.cljz.makepassenger.ui.activity.About1qActivity;
import com.cljz.makepassenger.utils.date.UpdataurlUtil;
import com.cljz.makepassenger.utils.date.VerCodeUitl;
import com.google.gson.Gson;

import java.util.HashMap;

import cn.droidlover.xdroidmvp.net.ApiSubcriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import cn.droidlover.xdroidmvp.net.diy.Base64Utils;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2017/4/7 0007.
 */

public class About1qP extends BaseP<About1qActivity> {

    public void About1q() {

        Gson gson = new Gson();
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("p_type", "android");
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().getNew_Version(requestBody)
                .compose(XApi.<About1qBean>getApiTransformer())         //异常处理
                .compose(XApi.<About1qBean>getScheduler())              //线程切换
                .compose(getV().<About1qBean>bindToLifecycle())
                .subscribe((new ApiSubcriber<About1qBean>() {
                    @Override
                    public void onNext(About1qBean about1qBean) {
                        //如果版本大于当前的，就换网上的版本,这里获取有问题
                        if ("0".equals(about1qBean.getError_code())) {
                            String serviceVersion = about1qBean.getData().getVersion();
                            if (Integer.parseInt(serviceVersion) > VerCodeUitl.getVerCode(App.getInstance())) {
                                UpdataurlUtil.setUrl(about1qBean.getData().getUrl());
                            } else {
                               // T.getInstance().showShort("已经是最新版本哦");
                            }
                            getV().About1qSucceed();
                        } else {
                            getV().About1qFail(about1qBean.getMsg());
                        }
                    }

                    @Override
                    protected void onFail(NetError error) {
//                        getV().About1qFail(error.toString());
                    }

                }));
    }
}
